package com.qsign.sfrz_android.activity.home.ViewController;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.device.ScanDevice;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.qsign.sfrz_android.R;
import com.qsign.sfrz_android.activity.home.Model.FuheModel;
import com.qsign.sfrz_android.base.NewBaseActivity;
import com.qsign.sfrz_android.net.IServiceImpl;
import com.qsign.sfrz_android.net.RequestServiceImpl;
import com.qsign.sfrz_android.publicview.SimpleDialog;
import com.qsign.sfrz_android.utils.BaseUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class ScanFuheActivity extends NewBaseActivity {
    @BindView(R.id.chukuedit)
    EditText chukuedit;

    private final static String SCAN_ACTION = "scan.rcv.message";
    @Override
    protected void initTitle(TextView textView) {
        textView.setText("棉纱入库复核");
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_scan_fuhe;
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mScanReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SCAN_ACTION);
        registerReceiver(mScanReceiver, filter);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        chukuedit.setFocusableInTouchMode(true);
        chukuedit.requestFocus();
    }

    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(chukuedit.hasFocus()){
                byte[] barocode = intent.getByteArrayExtra("barocode");
                int barocodelen = intent.getIntExtra("length", 0);
                String barcodeStr = new String(barocode, 0, barocodelen);
                chukuedit.setText(barcodeStr);
                showWait();
                RequestServiceImpl.fh_query(mMIPHandler, IServiceImpl.FH_QUERY,chukuedit.getEditableText().toString());
            }

        }
    };

    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
        stopWait();
        String msg = "";
        String opt = "";
        switch (requestType) {
            case IServiceImpl.FH_QUERY://根据条件查询
                try {
                    JSONObject jsonObject = JSONObject.parseObject(objBody.toString());
                    msg = jsonObject.getString("MSG_INFO");
                    opt = jsonObject.getString("OPT_FLAG");
                    if ("SUCCESS".equals(opt)) {
                        JSONArray dataarray = jsonObject.getJSONArray("DATA");
                        if (dataarray != null && dataarray.size() > 0) {
                            Intent inte=new Intent(this,FuheActivity.class);
                            inte.putExtra("fuhebizno",chukuedit.getEditableText().toString());
                            startActivity(inte);
                        }

                    } else {
                        if (TextUtils.isEmpty(msg)) {
                            showSimpleDialog("查询出错");
                        } else {
                            showSimpleDialog(msg);
                        }

                    }

                } catch (Exception e) {
                }
                break;
        }
    }
    private void showSimpleDialog(String message) {
        SimpleDialog.show(this, message);
    }
    @OnClick({R.id.backbtn, R.id.nextbtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backbtn:
                finish();
                break;
            case R.id.nextbtn:
                if(chukuedit.getEditableText().toString().length()==0){
                    BaseUtil.ShowToast(this,"请先扫描出库单号");
                    return;
                }
                showWait();
                RequestServiceImpl.fh_query(mMIPHandler, IServiceImpl.FH_QUERY,chukuedit.getEditableText().toString());
                break;
        }
    }
}
